/*
 * @Description:
 * @Autor: 曹敬涛
 * @Date: 2021-09-06 15:05:31
 * @LastEditors: 商晓彬
 * @LastEditTime: 2021-12-24 17:51:51
 */
import { LazyLoadEvent } from "primeng/api";
import { Component, Injector, ViewChild } from "@angular/core";
import { AppComponentBase } from "@shared/common/app-component-base";
import { Table } from "primeng/components/table/table";
import { Paginator } from "primeng/components/paginator/paginator";
import { MessageService } from "@abp/message/message.service";
import {
    StoreFunctionsServiceProxy, CollectBomServiceProxy, BomAnalyseServiceProxy, MaterialsOtherDto,
    OperationHistoryServiceProxy, StoreStockContentDto, CommonServiceProxy, MakeTaskServiceProxy
} from "@shared/service-proxies/service-proxies";
import { finalize } from "rxjs/operators";
import { FileDownloadService } from "@shared/utils/file-download.service";
import { UndoManager } from "gojs";

@Component({
    selector: "app-supporting-analysis",
    templateUrl: "./supporting-analysis.component.html",
    styleUrls: ["./supporting-analysis.component.less"],
})
export class SupportingAnalysisComponent extends AppComponentBase {
    @ViewChild("paginator") paginator: Paginator;
    @ViewChild("dataTable") dataTable: Table;
    model = undefined;
    createModelOption = [];
    LotNoList = [];
    createLotNoList = [];
    createCoreList = [];
    rangeDates: Date[]
    selectedCars3: any = [];
    logList: any;
    llVisible: boolean = false
    autoVisible: boolean = false
    addVisible: boolean = false
    queryInfo = {
        module: undefined,
        sendTime: undefined,
        xJName: undefined,
        xJCode: {
            label: undefined,
            value: undefined
        },
        xJId: undefined,
        materialPropertyName: undefined,
        preSuiteStatus: undefined,
        storeId: undefined,
    }
    storeDropList: MaterialsOtherDto[];
    detailInfo: any = {
        info: undefined,
        list: []
    }
    detailVisible: boolean = false
    queryText = {
        module: undefined,
        sendTime: undefined,
        xJName: undefined,
        xJCode: undefined,
        xJId: undefined,
        materialPropertyName: undefined,
        preSuiteStatus: undefined,
        storeId: undefined,
    }
    rengongSelect: any = []
    dialogType: string = "人工预配套"
    substitutions = {
        stroeName: undefined,//仓库名称
        materialNo: undefined,//物料图号
        stroeId: undefined,//物料图号
        materialName: undefined,//物料名称
        module: undefined,//规格
        materialTypeName: undefined,//物料类别
        qualifiedNumber: undefined,//合格证号
        releaseOrder: undefined,//放行单号
        hJXH: undefined,//火箭型号
        hJFC: undefined,//火箭发次
    }
    tdlist: StoreStockContentDto[];
    tdlistSelect: any = []
    ccc: StoreStockContentDto[];
    saveFlag = true;
    cpreSuiteStatusOptions = [
        {
            label: '仓库无物料',
            value:'仓库无物料'
        },{
            label: '缺料',
            value:'缺料'
        },{
            label: '未预配套',
            value:'未预配套'
        },{
            label: '预配套中',
            value:'预配套中'
        },{
            label: '已预配套',
            value:'已预配套'
        },
    ]
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _storeFunctionsServiceProxy: StoreFunctionsServiceProxy,
        private _collectBomServiceProxy: CollectBomServiceProxy,
        private _bomAnalyseServiceProxy: BomAnalyseServiceProxy,
        private _operationHistoryServiceProxy: OperationHistoryServiceProxy,
        private _fileDownloadService: FileDownloadService,
        private _commonServiceProxy: CommonServiceProxy,
        private _makeTaskServiceProxy: MakeTaskServiceProxy,
    ) {
        super(injector);
    }
    ngOnInit() {
        this.getModelList();
        this.getStoreDropListEv()
    }

    //履历
    llVisibleEv(id) {
        this.llVisible = true
        this._operationHistoryServiceProxy.getOperationLogList(
            id
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.logList = result
            });

    }
    //获取仓库下拉
    getStoreDropListEv(): void {
        this._storeFunctionsServiceProxy.getStoreDropList(
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.storeDropList = result
            });
    }
    // 全选
    selectedCars3All(): void {
        this.selectedCars3 = this.primengTableHelper.records
    }
    //弹窗隐藏
    displayclose(): void {
        this.detailInfo = {
            info: undefined,
            list: []
        }
    }
    //预配套
    autoEv(): void {
        if (this.selectedCars3.length === 0) {
            this._messageService.warn("请先选择数据")
            return
        }
        this.autoVisible = true
    }
    // 确定预配套
    autoPeiTaoEv() {
        // this._messageService.confirm(
        //     '是否确定自动预配套！！！！',
        //     (isConfirmed) => {
        //         if (isConfirmed) {
        abp.ui.setBusy();
        this._bomAnalyseServiceProxy.executePreSuite(
            this.selectedCars3
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                abp.ui.clearBusy();
                this.autoVisible = false
                this.selectedCars3 = []
                this.getDescList()
                if (result.code == 0) {
                    this._messageService.success(result.message);
                } else {
                    this._messageService.error(result.message);
                }

            });
        //         }
        //     }
        // );

    }
    //取消预配套
    cancelAutoEv(): void {
        if (this.selectedCars3.length === 0) {
            this._messageService.warn("请先选择数据")
            return
        }
        // this._messageService.confirm(
        //     '是否确定取消所选物料预配套！！！！',
        //     (isConfirmed) => {
        //         if (isConfirmed) {
        if (this.saveFlag) {
            this.saveFlag = false;
            this._bomAnalyseServiceProxy.cancelPreSuite(
                this.selectedCars3
            )
                .pipe(finalize(() => { }))
                .subscribe(result => {
                    this.saveFlag = true;
                    if (result.code == 0) {
                        this._messageService.success('操作成功')
                        this.selectedCars3 = []
                        this.getDescList()
                    } else {
                        this._messageService.error(result.message)
                    }

                });
        }
        //         }
        //     }
        // );

    }
    //导出预配套
    exportAuto(): void {
        if (this.selectedCars3.length === 0) {
            this._messageService.warn("请先选择数据")
            return
        }
        // this._messageService.confirm(
        //     '是否确定导出预配套！！！！',
        //     (isConfirmed) => {
        //         if (isConfirmed) {
        // if (this.saveFlag) {
        //     this.saveFlag = false;
        //接口导出预配套
        this._bomAnalyseServiceProxy.getBomAnalyseContentToExcel(
            this.selectedCars3
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                // this.saveFlag = true;
                // this._fileDownloadService.downloadTempFile(result);
                window.open(result);
                URL.revokeObjectURL(result);
                this.selectedCars3 = []
            });
        //             // }
        //         }
        //     }
        // );
    }
    //导出缺料表
    exportWuliao(): void {
        if (this.selectedCars3.length === 0) {
            this._messageService.warn("请先选择数据")
            return
        }
        // this._messageService.confirm(
        //     '是否确定导出缺料表！！！！',
        //     (isConfirmed) => {
        //         if (isConfirmed) {
        // if (this.saveFlag) {
        //     this.saveFlag = false;
        //接口导出缺料表GetStarvingMaterialsToExcel
        this._bomAnalyseServiceProxy.getStarvingMaterialsToExcel(
            this.selectedCars3
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                // this.saveFlag = true;
                // this._fileDownloadService.downloadTempFile(result);
                window.open(result);
                URL.revokeObjectURL(result);
                this.selectedCars3 = []
            });
        // }
        //         }
        //     }
        // );

    }
    //添加替代料
    getaddList(): void {
        this.addVisible = true
        // * @param stroeName (optional) 
        // * @param stroeId (optional) 
        // * @param materialNo (optional) 
        // * @param materialName (optional) 
        // * @param module (optional) 
        // * @param materialTypeName (optional) 
        // * @param qualifiedNumber (optional) 
        // * @param releaseOrder (optional) 
        // * @param hJXH (optional) 
        // * @param hJXHId (optional) 
        // * @param hJFC (optional) 
        // * @param hJFCId (optional) 
        this._bomAnalyseServiceProxy.getExecutePreSuiteSubstitutions(
            this.substitutions.stroeName ? this.substitutions.stroeName.label : undefined,
            this.substitutions.stroeId ? this.substitutions.stroeName.value : undefined,
            this.substitutions.materialNo,
            this.substitutions.materialName,
            this.substitutions.module,
            this.substitutions.materialTypeName,
            this.substitutions.qualifiedNumber,
            this.substitutions.releaseOrder,
            this.substitutions.hJXH ? this.substitutions.hJXH.label : undefined,
            this.substitutions.hJXH ? this.substitutions.hJXH.value : undefined,
            this.substitutions.hJFC ? this.substitutions.hJFC.label : undefined,
            this.substitutions.hJFC ? this.substitutions.hJFC.value : undefined,
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.tdlist = result
            });

    }
    // 添加替代料  添加按钮
    addList(): void {
        this.tdlistSelect.forEach(element => {
            this.detailInfo.list.push(element)
        });
        this.detailInfo = this.detailInfo
        this.addVisible = false
    }
    //人工预配套信息
    getExecutePreSuitePersonEv(car, type) {
        this.dialogType = type
        this.detailInfo.info = car
        this._bomAnalyseServiceProxy.getExecutePreSuitePerson(
            car.storeStockId, car.collectBomManagerId
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.detailInfo.list = result
                this.rengongSelect = result.filter(item => {
                    return item.isPreSuit == 1
                })
                this.detailVisible = true
                this.getDescList()
            });
    }
    // 人工预配套配套和取消预配套

    executePreSuitePersonEv() {
        this.detailInfo.info.storeStockContentDtoList = this.rengongSelect
        this._bomAnalyseServiceProxy.executePreSuitePerson(
            this.detailInfo.info
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                if (result.code == 1) {
                    this._messageService.error(result.message);
                } else {
                    this._messageService.success('操作成功');
                    this.detailVisible = false
                    this.getDescList()
                }
            });
    }
    // 重置
    reset() {
        this.createLotNoList = [];
        this.substitutions = {
            stroeName: undefined,//仓库名称
            materialNo: undefined,//物料图号
            stroeId: undefined,//物料图号
            materialName: undefined,//物料名称
            module: undefined,//规格
            materialTypeName: undefined,//物料类别
            qualifiedNumber: undefined,//合格证号
            releaseOrder: undefined,//放行单号
            hJXH: undefined,//火箭型号
            hJFC: undefined,//火箭发次
        }
        this.queryInfo = {
            module: undefined,
            sendTime: undefined,
            xJName: undefined,
            xJCode: undefined,
            xJId: undefined,
            materialPropertyName: undefined,
            preSuiteStatus: undefined,
            storeId: undefined,
        }
        this.queryText = {
            module: undefined,
            sendTime: undefined,
            xJName: undefined,
            xJCode: undefined,
            xJId: undefined,
            materialPropertyName: undefined,
            preSuiteStatus: undefined,
            storeId: undefined,
        }
    }
    /**
     * 查询配套数据
     *  module:"", sendTime:"", xJName:"",  xJCode:"",  xJId:"",   materialPropertyName:"", 
    preSuiteStatus:"",   storeId:"",  sorting:"",  maxResultCount:"",  skipCount:"",
     */
    getDescList(event?: LazyLoadEvent): void {
        abp.ui.setBusy();
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this._bomAnalyseServiceProxy.getBomAalyseContent(
            this.queryInfo.module ? this.queryInfo.module.label : undefined,
            this.queryInfo.module ? this.queryInfo.module.value : undefined,
            this.queryInfo.sendTime ? this.queryInfo.sendTime.label : undefined,
            this.queryInfo.sendTime ? this.queryInfo.sendTime.value : undefined,
            this.queryInfo.xJCode.label,
            undefined,
            this.queryInfo.xJCode.value,
            this.queryInfo.materialPropertyName,
            this.queryInfo.preSuiteStatus,
            this.queryInfo.storeId,
            this.primengTableHelper.getSorting(this.dataTable),//undefined
            this.primengTableHelper.getMaxResultCount(this.paginator, event),//10
            this.primengTableHelper.getSkipCount(this.paginator, event)//0
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                abp.ui.clearBusy();
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }
    //获取左侧型号
    getModelList() {
        this._commonServiceProxy.getModelList(
        )
            .pipe(finalize(() => { })).subscribe(result => {
                this.createModelOption = result;
            })
    }

    //获取仓库名称
    getStoreName(e) {
        this.queryText.storeId = this.storeDropList.filter(item => {
            return item.value == e.value
        })[0].label


    }
    //根据型号获取对应的发次
    getLotNoList(event) {
        this._commonServiceProxy.getLotNoList(
            this.substitutions.hJXH.value
        )
            .pipe(finalize(() => { })).subscribe(result => {
                this.createLotNoList = result
            })
    }
    onChange(event) {
        this._commonServiceProxy.getLotNoList(
            this.queryInfo.module.value
        )
            .pipe(finalize(() => { })).subscribe(result => {
                this.createLotNoList = result
            })
    }
    //根据型号发次获取对应的芯级
    getCoreListCreate(e, type) {
        this.queryText.sendTime = e.label
        this._commonServiceProxy.getBomXj(this.queryInfo.module.value, this.queryInfo.sendTime.value, undefined, undefined,)
            .pipe(finalize(() => { })).subscribe(result => {
                this.createCoreList = result
            })
        this._makeTaskServiceProxy.getJobNumber(this.queryInfo.sendTime.value, this.queryInfo.module.value)
            .pipe(finalize(() => { })).subscribe(result => {
                this.queryInfo.storeId = result.storeId
            })

    }
    // 获取芯级的name
    getxjName(e) {
        this.queryText.xJCode = this.queryInfo.xJCode.label
    }
}
